<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0, maximum-scale=1, minimum-scale=1, user-scalable=no"
    />
    <title>选择收货地址</title>
    <link rel="stylesheet" type="text/css" href="../css/swiper.min.css" />
    <link rel="stylesheet" type="text/css" href="../css/dialog.css" />
    <link rel="stylesheet" type="text/css" href="../css/style.css" />
    <script src="../js/flexible.js" type="text/javascript"></script>
    <script src="../js/swiper.min.js" type="text/javascript"></script>
    <script src="../js/zepto-v1.2.0.min.js" type="text/javascript"></script>
    <script src="../js/dialog.min.js" type="text/javascript"></script>
  </head>
  <body>
    <div class="address-list-wrapper common-wrapper">
      <header class="header-wrapper clearfix">
        <div class="header-container white-background">
          <div class="left-container">
            <a class="back black-back" href="javascript:history.back(-1)"></a>
          </div>
          <span class="title">选择收货地址</span>
          <div class="right-container">
            <a class="edit" href="addAddress.html">新增地址</a>
          </div>
        </div>
      </header>
      <section class="empty-address-container">
        <div class="empty-address">
          <img
            src="../img/1x/empty-address.png"
            srcset="../img/2x/empty-address@2x.png 2x"
          />
          <p class="text">暂无收货地址</p>
        </div>
        <a class="normal-btn primary-btn" href="addAddress.html">新增收货地址</a>
      </section>
      <section class="address-list">
        <div class="address-item">
          <span class="checkbox"></span>
          <div class="address-info">
            <div class="address">杭州市拱墅区温州路71号南北商务港1单元8楼</div>
            <div class="user-info">
              <span class="name">周杰伦</span>
              <span class="tel">19889988888</span>
            </div>
          </div>
          <span class="edit"></span>
        </div>
        <div class="address-item">
          <span class="checkbox"></span>
          <div class="address-info">
            <div class="address">杭州市滨江区黄河街道长江小区34号楼5单元1201室</div>
            <div class="user-info">
              <span class="name">黄晓明</span>
              <span class="tel">18823217876</span>
            </div>
          </div>
          <span class="edit"></span>
        </div>
      </section>
    </div>
  </body>
  <script>
    $(document).ready(function () {
      const url_string = window.location.href
      const url = new URL(url_string)
      const hasAddress = url.searchParams.get('hasAddress')
      if (hasAddress === 'true') {
        $('.address-list').show()
        $('.empty-address-container').hide()
        $('.back').attr('href', 'submitOrder.html?hasAddress=true')
      }
      $('.address-item').click(function (e) {
        if (!$(e.target).hasClass('edit')) {
          $(this).children('.checkbox').toggleClass('checked')
        }
      })
      $('.edit').click(function () {
        const closestParent = $(this).siblings('.address-info')
        const address = $(closestParent).find('.address').html()
        const userName = $(closestParent).find('.name').html()
        const userTel = $(closestParent).find('.tel').html()
        console.log('address:', address, userName, userTel)
        const targetHref = `addAddress.html?status=edit&address=${address}&name=${userName}&tel=${userTel}`
        window.location.href = targetHref
      })
    })
  </script>
</html>
